package com.android.hw.projectmenu;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import android.os.SystemProperties;
import android.provider.Settings;


public class UsbSwitch extends Activity implements RadioGroup.OnCheckedChangeListener{
	private RadioButton google_mode;
	private RadioButton production_mode;
	private RadioButton other_mode;
	private RadioGroup usb_switch;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usb_switch);
		/*15-09-23 gaoshenpan not change USB when monkeyTest begin*/
		if(isMonkeyRunning()){
			this.finish();
		}
		/*15-09-23 gaoshenpan not change USB when monkeyTest end*/
		google_mode=(RadioButton)findViewById(R.id.google_mode);
		production_mode=(RadioButton)findViewById(R.id.production_mode);
		other_mode=(RadioButton)findViewById(R.id.other_mode);
		usb_switch=(RadioGroup)findViewById(R.id.usb_switch);
		updateView();
		usb_switch.setOnCheckedChangeListener(this);
	}
	/*15-09-23 gaoshenpan not change USB when monkeyTest begin*/
	public static boolean isMonkeyRunning() {
		return ActivityManager.isUserAMonkey();
	}
	/*15-09-23 gaoshenpan not change USB when monkeyTest end*/
	@Override
	protected void onResume() {
		super.onResume();
		if (isGoogleMode()) {
			google_mode.setChecked(true);
		} else if (isProductionMode()) {
			production_mode.setChecked(true);
		} else {
			other_mode.setChecked(true);
		}
	}
	
	private boolean isGoogleMode() {
		return "mass_storage".equals(SystemProperties.get("sys.usb.config", "")) ||
				"mass_storage,adb".equals(SystemProperties.get("sys.usb.config", ""));
	}
	
	private boolean isProductionMode() {
		return "diag,serial_smd,serial_tty,rmnet_bam,mass_storage,adb".equals(SystemProperties.get("sys.usb.config", ""));
	}

	public void updateView(){

	}
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
		case R.id.google_mode:
			int UsbDebugEnable= Settings.Global.getInt(getContentResolver(),Settings.Global.ADB_ENABLED, 0);
			if (UsbDebugEnable == 0) {
				SystemProperties.set("sys.usb.config", "mass_storage");
			} else {
				SystemProperties.set("sys.usb.config", "mass_storage,adb");
			}			
			break;
		case R.id.production_mode:
			SystemProperties.set("sys.usb.config", "diag,serial_smd,serial_tty,rmnet_bam,mass_storage,adb");
			break;
		case R.id.other_mode:
			
			break;
		}
		
	}
   

}
